R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## 
## Attaching package: 'data.table'
## 
## 
## The following objects are masked from 'package:lubridate':
## 
##     hour, isoweek, mday, minute, month, quarter, second, wday, week,
##     yday, year
## 
## 
## The following objects are masked from 'package:dplyr':
## 
##     between, first, last
## 
## 
## The following object is masked from 'package:purrr':
## 
##     transpose
## Warning: package 'plotly' was built under R version 4.4.2
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
## `summarise()` has grouped output by 'year', 'state', 'county'. You can override
## using the `.groups` argument.
plot1_data <- cra_assets %>%
  filter(state <= 56) %>%
  mutate(state = str_pad(state, width = 2, pad = "0")) %>% 
  group_by(year, portfolio) %>%
  summarize(n = n(),
            amt_us_sb_loans = sum(sum_loan_amt),
            n_us_sb_loans = sum(sum_n_loans))
## `summarise()` has grouped output by 'year'. You can override using the
## `.groups` argument.
plot1 <- ggplot(data = plot1_data) +
  geom_area(aes(x = year,
            y = amt_us_sb_loans,
            fill = portfolio, group = portfolio))

plot1

## WITHOUT grouping by portfolio
## Also takes forever to compile, so perhaps filter out some years?
## Also I made two plots: one with a log transform and one without.

# plot2_log <- county_acs_data |>
#   filter(!is.na(sum_loan_amt), !is.na(Median_HH_Income)) |>
#   ggplot() +
#   geom_point(mapping = aes(x = log(sum_loan_amt),
#                            y = log(Median_HH_Income),
#                            color = year,
#                            text = paste0("County: ", County, "<br>Total Loan Amount: ", sum_loan_amt, "<br>Median Household Income: ", Median_HH_Income, "<br>Year:", year))) +
#   labs(x = "Log of Loan Amount Sum",
#        y = "Log of Median Household Income",
#        title = "Loan Amounts vs. Median Household Income")

plot2 <- county_acs_data |>
  filter(!is.na(sum_loan_amt), !is.na(Median_HH_Income)) |>
  ggplot() +
  geom_point(mapping = aes(x = sum_loan_amt,
                           y = Median_HH_Income,
                           frame = year,
                           # color = year,
                           text = paste0("County: ", County, "<br>Total Loan Amount: ", sum_loan_amt, "<br>Median Household Income: ", Median_HH_Income, "<br>Year:", year))) +
  labs(x = "Loan Amount Sum",
       y = "Median Household Income",
       title = "Loan Amounts vs. Median Household Income")
## Warning in geom_point(mapping = aes(x = sum_loan_amt, y = Median_HH_Income, :
## Ignoring unknown aesthetics: frame and text
ggplotly(plot2,
         tooltip = "text")

Plot 3

## WITH portfolio grouping

(plot3 <- county_acs_data |>
  filter(!is.na(year), !is.na(sum_n_loans), !is.na(sum_loan_amt)) |>
  group_by(portfolio, year) |>
  summarize(total_sum_n_loans = sum(sum_n_loans),
            avg_sum_loan_amounts = mean(sum_loan_amt)) |>
  ggplot() +
  geom_line(mapping = aes(x = year, y = avg_sum_loan_amounts, color = portfolio)) +
  labs(x = "Year",
       y = "Average Loan Amount",
       title = "Total Loans Given Out Each Year by Bank Type"))
## `summarise()` has grouped output by 'portfolio'. You can override using the
## `.groups` argument.